﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net.TheBoredCoders.SoundBoard
{
    public partial class SettingsForm : Form
    {
        private bool settingsChanged;
        private string currentSamplesDirectory;

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsForm"/> class.
        /// </summary>
        public SettingsForm()
        {
            InitializeComponent();
            // remind the current samples-dir for the case of changes
            currentSamplesDirectory = Properties.Settings.Default.Path_Content;
        }

        /// <summary>
        /// Handles the Load event of the SettingsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SettingsForm_Load(object sender, EventArgs e)
        {
            AppSettings settingsLink = new AppSettings();
            propertyGrid.SelectedObject = settingsLink;

            Properties.Settings.Default.SettingChanging += new System.Configuration.SettingChangingEventHandler(Default_SettingChanging);
            this.settingsChanged = false;
        }

        /// <summary>
        /// Handles the SettingChanging event of the Default control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Configuration.SettingChangingEventArgs"/> instance containing the event data.</param>
        void Default_SettingChanging(object sender, System.Configuration.SettingChangingEventArgs e)
        {
            this.settingsChanged = true;
            if (e.SettingName == "Path_Content") // if path has changed
            {
                contentDirectoryChanged(e.NewValue.ToString());
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the SettingsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (settingsChanged)
            {
                if (MessageBox.Show("Do you want to save the Settings?", "Changes detected:", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Properties.Settings.Default.Reload();
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Code for the case that the content directory changed.
        /// </summary>
        /// <param name="newPath">The new path.</param>
        private void contentDirectoryChanged(string newPath)
        {
            // create Message
            StringBuilder tmpMessage = new StringBuilder();
            tmpMessage.AppendLine("You have changed the Content-Directory.");
            tmpMessage.AppendLine("");
            tmpMessage.AppendLine("Do you want to move (or just copy) your current samples to the new location?");
            tmpMessage.AppendLine("");
            tmpMessage.AppendLine("Use \"Yes\" to move, \"No\" to copy or \"Cancel\" to do nothing with the current samples...");
            // ask user if he wants to use the copy-service for the current samples
            switch (MessageBox.Show(tmpMessage.ToString(), "Use the Copy-Service?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            {
                case System.Windows.Forms.DialogResult.Yes: // move the files
                    CopyForm tmpDialog1 = new CopyForm(true, currentSamplesDirectory, newPath);
                    currentSamplesDirectory = newPath; // save new current path for next change
                    tmpDialog1.ShowDialog();
                    break;
                case System.Windows.Forms.DialogResult.No: // copy the files
                    CopyForm tmpDialog2 = new CopyForm(false, currentSamplesDirectory, newPath);
                    currentSamplesDirectory = newPath; // save new current path for next change
                    tmpDialog2.ShowDialog();
                    break;
                default: // do nothing
                    break;
            }
        }
    }
}